﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Runtime.Serialization;
using System.Globalization;
using Common;
using System.Xml.Serialization;

namespace Model
{
    public class TdyxObjects
    {
        protected long id;//物品唯一识别编号
        protected string name;
        protected byte[] code;
        private string codeString = "";//对象代码的字符串形式，例如：0E 00

        /// <summary>
        /// 对象代码的字符串形式
        /// </summary>
        public string CodeString
        {
            get
            {
                if (codeString == "")
                {
                    foreach (byte b in code)
                    {
                        codeString += b.ToString("X2") + " ";
                    }

                    if (codeString.Length > 0)
                    {
                        codeString = codeString.Substring(0, codeString.Length - 1);
                    }
                }
                return codeString;
            }
            set
            {
                codeString = value;
                string[] temp = codeString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int index = 0;
                code = new byte[temp.Length];
                for (int i = 0; i < temp.Length; i++)
                {
                    if (!byte.TryParse(temp[i], NumberStyles.AllowHexSpecifier, null, out code[i]))
                    {
                        Tools.PrintException(new Exception("进行对象代码字符串到字节数组的转换时出错，对象数据可能不能正常使用。"));
                    }
                }
            }
        }

        /// <summary>
        /// 物品唯一识别编号
        /// </summary>
        public long ID
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 代码
        /// </summary>
        [XmlIgnore]
        public byte[] Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        /// <summary>
        /// 构造一个天地游戏英雄对象
        /// </summary>
        public TdyxObjects()
        {
        }

        /// <summary>
        /// 构造一个天地游戏英雄对象
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="code">代码</param>
        public TdyxObjects(string name, byte[] code)
        {
            this.name = name;
            this.Code = code;
        }
    }
}
